# delimit ;
clear ;
cd "replication" ;
set more off ; 

* *************************************************************************** ;
* re-run midline analysis
* *************************************************************************** ;

cd ".." ;
do "./replication/do/table03-midline-knowledge-aggregate.do" ; 

* **************************************************************************** ;
* merge with baseline to get full sample list including attriters
* **************************************************************************** ;

merge 1:1 Id using "./data/baseline", keepusing(Id wave class neighborhood female mfi healthonly healthandpay control) ;
tab _merge ;
assert _merge != 1 ; 

* selection indicator to be used in Lee bounds ;
gen selection_indicator = _merge == 3 ; 

* logical check: full sample is 887 respondents ;
qui des ;
assert r(N) == 887 ;

****************************************************************************** ;
* generate randomization strata and wave-class variable
****************************************************************************** ;

* need to do this again so that the attriters also have values for strata/waveclass ;
drop strata waveclass ;

egen strata = group(wave neighborhood female mfi) ;
egen waveclass = group(wave class) ;

* *****************************************************************************
* robustness: extreme value (manski) bounds 
* ***************************************************************************** ;
est drop _all ;

foreach var of varlist score_tot score_cleanliness score_midwife score_condom { ; 

	* lower bound ; 
	gen l`var' = `var' ;
	replace l`var' = 0 if (healthonly == 1 | healthandpay == 1) & missing(`var') ;
	replace l`var' = 1 if (healthonly == 0 & healthandpay == 0) & missing(`var') ; 
	label var l`var' "Lower" ;
	
	areg l`var' healthonly healthandpay, a(strata) robust ;
	est sto `var'_manskil ;
	
	* upper bound ; 
	gen u`var' = `var' ;
	replace u`var' = 1 if (healthonly == 1 | healthandpay == 1) & missing(`var') ;
	replace u`var' = 0 if (healthonly == 0 & healthandpay == 0) & missing(`var') ; 
	
	areg u`var' healthonly healthandpay, a(strata) robust ;
	est sto `var'_manskiu ; 
	label var u`var' "Upper" ;
	
} ;

* ***************************************************************************** ;
* output, panel A: results from manski bounds
* ***************************************************************************** ;

esttab *manski* using "./output/appendix-table-midline-knowledge-bounds-panelA.tex",
	replace
	drop(_cons)
	cells(b(label() star fmt(%9.3f %9.3f)) se(par)) 
	star(* 0.10 ** 0.05 *** 0.01) 
	stats(N, fmt(%9.0f) labels("N"))	substitute(\_ _)
	prehead(\begin{table}[p] \centering `"\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}"'
		\captionsetup{justification=centering} 
		\caption{Bounds for Effects on Short-Term Knowledge}
		\label{appendix-table-midline-bounds}
	\setlength{\tabcolsep}{23pt}
	\begin{tabular}{p{0cm}p{4cm}p{3cm}p{3cm}p{3cm}p{3cm}p{3cm}p{3cm}p{3cm}}
		\toprule)
	legend label booktabs collabels( , none)
	mgroups(
	"\shortstack[c]{All Topics}"
	"\shortstack[c]{Cleanliness \\and Hygiene}"
	"\shortstack[c]{Midwives, Maternal \\ and Child Health}"
	"\shortstack[c]{Condoms, AIDS, \\Syphilis}"
	, pattern(1 0 1 0 1 0 1 0)  prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))
	postfoot(`"\midrule"'  \end{tabularx}) 
	posthead(\midrule \end{tabular}
	\bigskip
	\caption*{Panel A: Manski Bounds} 
	\vspace{-0.5ex}
	\newcolumntype{C}{>{\centering\arraybackslash}X} \setlength{\tabcolsep}{6pt} 
	\begin{tabularx}{\linewidth}{lCCCCCCCC}
	\midrule
	&\multicolumn{1}{c}{(1)} &\multicolumn{1}{c}{(2)} &\multicolumn{1}{c}{(3)}
	&\multicolumn{1}{c}{(4)} &\multicolumn{1}{c}{(5)} &\multicolumn{1}{c}{(6)}
	&\multicolumn{1}{c}{(7)} &\multicolumn{1}{c}{(8)}
	\\ \midrule) ;

* *****************************************************************************
* robustness: lee bounds
* ***************************************************************************** ;

* save results within the data, which I will then output using texsave ;
gen variable = "" ; 
replace variable = "HEE" in 1 ;
replace variable = "HEEC" in 3 ;
label var variable "" ;

foreach var of varlist score* { ;
	gen `var'_l = "" ;
	gen `var'_u = "" ;
	
	label var `var'_l "" ;
	label var `var'_u "" ;
} ;

foreach var of varlist score_tot score_cleanliness score_midwife score_condom { ; 

	di "************************* outcome: `var' *************************" ;

	* for the variable healthonly (HEE), calculate lee bounds ;
	* HEE arm vs. control arm, so HEEC arm is excluded from the command ; 
	
	leebounds `var' healthonly if healthonly == 1 | control == 1 , select(selection_indicator) ; 
	
	* get coeffs ;
	replace `var'_l  = string(e(b)[1,1], "%9.3f") in 1 ;
	replace `var'_u  = string(e(b)[1,2], "%9.3f") in 1 ; 
	
	* get SEs ;
	replace `var'_l = "(" + string(sqrt(e(V)[1,1]), "%9.3f") + ")" in 2 ; 
	replace `var'_u = "(" + string(sqrt(e(V)[2,2]), "%9.3f") + ")" in 2 ; 
	
	* add stars  ; 
	if e(b)[1,1]/sqrt(e(V)[1,1]) >= 1.64 replace `var'_l  = string(e(b)[1,1], "%9.3f") + "*" in 1 ;
	if e(b)[1,1]/sqrt(e(V)[1,1]) >= 1.96 replace `var'_l  = string(e(b)[1,1], "%9.3f") + "**" in 1 ;
	if e(b)[1,1]/sqrt(e(V)[1,1]) >= 2.58 replace `var'_l  = string(e(b)[1,1], "%9.3f") + "***" in 1 ;

	if e(b)[1,2]/sqrt(e(V)[2,2]) >= 1.64 replace `var'_u  =  string(e(b)[1,2], "%9.3f")  + "*" in 1 ;
	if e(b)[1,2]/sqrt(e(V)[2,2]) >= 1.96 replace `var'_u  =  string(e(b)[1,2], "%9.3f") + "**" in 1 ;
	if e(b)[1,2]/sqrt(e(V)[2,2]) >= 2.58 replace `var'_u  =  string(e(b)[1,2], "%9.3f")  + "***" in 1 ;

	* for the variable healtandpay (HEEC), calculate lee bounds ;
	* HEEC arm vs. control arm, so HEE arm is excluded from the command ; 
	
	leebounds `var' healthandpay if healthandpay == 1 | control == 1, select(selection_indicator) ; 
	
	* get coeffs ;
	replace `var'_l  = string(e(b)[1,1], "%9.3f") in 3 ;
	replace `var'_u  = string(e(b)[1,2], "%9.3f") in 3 ; 
	
	* get SEs; 
	replace `var'_l = "(" + string(sqrt(e(V)[1,1]), "%9.3f") + ")"  in 4 ; 
	replace `var'_u = "(" + string(sqrt(e(V)[2,2]), "%9.3f") + ")" in 4 ; 	

	* add stars  ; 
	if e(b)[1,1]/sqrt(e(V)[1,1]) >= 1.64 replace `var'_l  = string(e(b)[1,1], "%9.3f") + "*" in 3 ;
	if e(b)[1,1]/sqrt(e(V)[1,1]) >= 1.96 replace `var'_l  = string(e(b)[1,1], "%9.3f") + "**" in 3 ;
	if e(b)[1,1]/sqrt(e(V)[1,1]) >= 2.58 replace `var'_l  = string(e(b)[1,1], "%9.3f") + "***" in 3 ;

	if e(b)[1,2]/sqrt(e(V)[2,2]) >= 1.64 replace `var'_u  =  string(e(b)[1,2], "%9.3f")  + "*" in 3 ;
	if e(b)[1,2]/sqrt(e(V)[2,2]) >= 1.96 replace `var'_u  =  string(e(b)[1,2], "%9.3f") + "**" in 3 ;
	if e(b)[1,2]/sqrt(e(V)[2,2]) >= 2.58 replace `var'_u  =  string(e(b)[1,2], "%9.3f")  + "***" in 3 ;

} ;

* ***************************************************************************** ;
* output, panel B: results from lee bounds
* ***************************************************************************** ;

keep variable score_tot_l score_tot_u score_cleanliness_l score_cleanliness_u score_midwife_l score_midwife_u score_condom_l score_condom_u ;
dropmiss, obs force ;
texsave using "./output/appendix-table-midline-knowledge-bounds-panelB.tex", frag replace title("Panel B: Lee Bounds") autonumber varlabels ;

filefilter "./output/appendix-table-midline-knowledge-bounds-panelB.tex" "./output/appendix-table-midline-knowledge-bounds-panelB-v00.tex", from("\BStoprule") to("\BSmidrule") replace ;
filefilter "./output/appendix-table-midline-knowledge-bounds-panelB-v00.tex" "./output/appendix-table-midline-knowledge-bounds-panelB-v01.tex", from("\BSbegin{table}[tbp] \BScentering") to("\BSbigskip") replace ;
filefilter "./output/appendix-table-midline-knowledge-bounds-panelB-v01.tex" "./output/appendix-table-midline-knowledge-bounds-panelB-v02.tex", from("{}&{}&{}&{}&{}&{}&{}&{}&{} \BStabularnewline") to("") replace ;
filefilter "./output/appendix-table-midline-knowledge-bounds-panelB-v02.tex" "./output/appendix-table-midline-knowledge-bounds-panelB-v03.tex", from("\BSmidrule \BSaddlinespace[\BSbelowrulesep]") to("") replace ;
filefilter "./output/appendix-table-midline-knowledge-bounds-panelB-v03.tex" "./output/appendix-table-midline-knowledge-bounds-panelB-v04.tex", 
from("\BScaption{Panel B: Lee Bounds}") to("\BScaption*{Panel B: Lee Bounds}") replace ;
filefilter "./output/appendix-table-midline-knowledge-bounds-panelB-v04.tex" "./output/appendix-table-midline-knowledge-bounds-panelB.tex", 
from("\BSend{table}") to("\BScaptionsetup{justification=justified, width=\BSlinewidth} \BScaption*{\BSfootnotesize \BStextit{Notes:} This table presents bounds on the treatment effects estimated in Table \BSref{table-midline-knowledge-aggregate}. Panel A uses extreme value (Manski) bounds following \BStextcite{horowitz2000nonparametric}. Panel B uses trimming bounds following \BStextcite{lee2009training} and is estimated with no covariates.} \BSend{table}") replace ;

forvalues i = 0/4 { ;
	erase "./output/appendix-table-midline-knowledge-bounds-panelB-v0`i'.tex" ;
} ;

exit ;
